# -*- coding:utf-8 -*-
# created on 2017/4/30
# 

from mathsolver.functions.base import *
from mathsolver.functions.base.base import new_latex


# 直角三角形建系
class XLBuildRectangularTriangleAxis(BaseFunction):
    def solver(self, *args):
        name = args[0]
        san_jiao_xing = self.search(name)
        zhi_jiao_ding_dian = san_jiao_xing.Angle_Rectangular_name
        point_a_name = san_jiao_xing.point_A_name
        point_b_name = san_jiao_xing.point_B_name
        point_c_name = san_jiao_xing.point_C_name
        line_to_a_name = san_jiao_xing.line_To_A_name
        line_to_c_name = san_jiao_xing.line_To_C_name
        line_to_b_name = san_jiao_xing.line_To_B_name
        line_to_a_value = san_jiao_xing.line_To_A_value
        line_to_b_value = san_jiao_xing.line_To_B_value
        line_to_c_value = san_jiao_xing.line_To_C_value
        self.steps.append(["", "设%s = %s, %s = %s, %s = %s, 得" % (new_latex(line_to_a_name), new_latex(line_to_a_value),
                                                                    new_latex(line_to_b_name), new_latex(line_to_b_value),
                                                                    new_latex(line_to_c_name), new_latex(line_to_c_value))])
        v_eqs = san_jiao_xing.Eqs
        v_eqs.append([line_to_a_value, ">", S.Zero])
        v_eqs.append([line_to_b_value, ">", S.Zero])
        v_eqs.append([line_to_c_value, ">", S.Zero])
        if zhi_jiao_ding_dian == point_a_name:
            self.steps.append(["", "以%s为坐标原点, %s为x轴,%s为y轴建立直角坐标系, 则" % (new_latex(point_a_name), new_latex(line_to_c_name), new_latex(line_to_b_name))])
            point_a_axis = BasePoint({"name": point_a_name, "value": [0, 0]})
            san_jiao_xing.point_A_Axis = point_a_axis
            point_b_axis = BasePoint({"name": point_b_name, "value": [line_to_c_value, 0]})
            san_jiao_xing.point_B_Axis = point_b_axis
            point_c_axis = BasePoint({"name": point_c_name, "value": [0, line_to_b_value]})
            san_jiao_xing.point_C_Axis = point_c_axis
            self.steps.append(["", "%s, %s, %s" % (point_a_axis.printing(), point_b_axis.printing(), point_c_axis.printing())])
        elif zhi_jiao_ding_dian == point_b_name:
            self.steps.append(["", "以%s为坐标原点, %s为x轴,%s为y轴建立直角坐标系, 则" % (new_latex(point_b_name), new_latex(line_to_a_name), new_latex(line_to_c_name))])
            point_a_axis = BasePoint({"name": point_a_name, "value": [0, line_to_c_value]})
            san_jiao_xing.point_A_Axis = point_a_axis
            point_b_axis = BasePoint({"name": point_b_name, "value": [0, 0]})
            san_jiao_xing.point_B_Axis = point_b_axis
            point_c_axis = BasePoint({"name": point_c_name, "value": [line_to_a_value, 0]})
            san_jiao_xing.point_C_Axis = point_c_axis
            self.steps.append(["", "%s, %s, %s" % (point_a_axis.printing(), point_b_axis.printing(), point_c_axis.printing())])
        else:
            self.steps.append(["", "以%s为坐标原点, %s为x轴,%s为y轴建立直角坐标系, 则" % (new_latex(point_c_name), new_latex(line_to_a_name), new_latex(line_to_b_name))])
            point_a_axis = BasePoint({"name": point_a_name, "value": [0, line_to_b_value]})
            san_jiao_xing.point_A_Axis = point_a_axis
            point_b_axis = BasePoint({"name": point_b_name, "value": [line_to_a_value, 0]})
            san_jiao_xing.point_B_Axis = point_b_axis
            point_c_axis = BasePoint({"name": point_c_name, "value": [0, 0]})
            san_jiao_xing.point_C_Axis = point_c_axis
            self.steps.append(["", "%s, %s, %s" % (point_a_axis.printing(), point_b_axis.printing(), point_c_axis.printing())])
        self.output.append(san_jiao_xing)
        if san_jiao_xing.type == "vIsoscelesRectangularTriangle":
            self.label.add("建系-等腰直角三角形")
        else:
            self.label.add("建系-直角三角形")
        return self


# 等边三角形建系
class XLBulidRegularTriangleAxis(BaseFunction):
    def solver(self, *args):
        name = args[0]
        san_jiao_xing = self.search(name)
        point_a_name = san_jiao_xing.point_A_name
        point_b_name = san_jiao_xing.point_B_name
        point_c_name = san_jiao_xing.point_C_name
        line_to_a_name = san_jiao_xing.line_To_A_name
        edge_length = san_jiao_xing.edge_length
        v_eqs = san_jiao_xing.Eqs
        if not edge_length:
            edge_length = sympify('a')
            self.steps.append(["", "设等边三角形的边长为%s, 得" % new_latex(edge_length)])
            v_eqs.append([edge_length, ">", S.Zero])
        san_jiao_xing.line_To_A_value = edge_length
        san_jiao_xing.line_To_B_value = edge_length
        san_jiao_xing.line_To_C_value = edge_length
        self.steps.append(["", "以%s所在线段为x轴,%s所在线段的中垂线为y轴建立坐标系,得" % (new_latex(line_to_a_name), new_latex(line_to_a_name))])
        point_a_axis = BasePoint({"name": point_a_name, "value": [0, sqrt(3) * edge_length / 2]})
        point_b_axis = BasePoint({"name": point_b_name, "value": [- edge_length / 2, 0]})
        point_c_axis = BasePoint({"name": point_c_name, "value": [edge_length / 2, 0]})
        self.steps.append(["", "∴ %s, %s, %s" % (point_a_axis.printing(), point_b_axis.printing(), point_c_axis.printing())])
        san_jiao_xing.point_A_Axis = point_a_axis
        san_jiao_xing.point_B_Axis = point_b_axis
        san_jiao_xing.point_C_Axis = point_c_axis
        self.label.add("建系-等边三角形")
        self.output.append(san_jiao_xing)
        return self
